package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemRoleButtonDao;
import com.example.child_protection.dto.info.SystemRoleButtonDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemRoleButtonRequestDTO;
import com.example.child_protection.dto.response.SystemRoleButtonListResponseDTO;
import com.example.child_protection.dto.response.SystemRoleButtonResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemRoleButtonModel;
import com.example.child_protection.service.SystemRoleButtonQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 角色按钮查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemRoleButtonQueryServiceImpl extends MyBaseService implements SystemRoleButtonQueryService {

    @Autowired
    public SystemRoleButtonDao systemRoleButtonDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleButtonResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_ROLE_BUTTON_FIND_ID, requestDTO, SystemRoleButtonResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemRoleButtonResponseDTO>() {
            @Override
            protected SystemRoleButtonResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemRoleButtonResponseDTO responseDTO = new SystemRoleButtonResponseDTO();
                SystemRoleButtonModel systemRoleButtonModel = systemRoleButtonDao.findId(requestDTO.getId());
                SystemRoleButtonDTO systemRoleButton = new SystemRoleButtonDTO();
                BeanUtils.copyProperties(systemRoleButtonModel, systemRoleButton);
                responseDTO.setInfo(systemRoleButton);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleButtonListResponseDTO findList(SystemRoleButtonRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_ROLE_BUTTON_FIND_LIST, requestDTO, SystemRoleButtonListResponseDTO.class, new MyServiceExecutor<SystemRoleButtonRequestDTO, SystemRoleButtonListResponseDTO>() {
            @Override
            protected SystemRoleButtonListResponseDTO myprocess(SystemRoleButtonRequestDTO requestDTO) throws Exception {
                SystemRoleButtonListResponseDTO responseDTO = new SystemRoleButtonListResponseDTO();
                SystemRoleButtonModel req = new SystemRoleButtonModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemRoleButtonModel> lists = systemRoleButtonDao.findPage(req);
                List<SystemRoleButtonDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemRoleButtonDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
